package com.ax.associator.store.service.impl;

import com.ax.associator.store.common.utils.BirthUtil;
import com.ax.associator.store.common.utils.ErrorUtil;
import com.ax.associator.store.common.utils.PageUtils;
import com.ax.associator.store.common.utils.Query;
import com.ax.associator.store.dao.AttachmentMapper;
import com.ax.associator.store.dao.BaseAssociatorMapper;
import com.ax.associator.store.dao.BusiAttachRelationMapper;
import com.ax.associator.store.domain.Attachment;
import com.ax.associator.store.domain.BaseAssociator;
import com.ax.associator.store.domain.BusiAttachRelation;
import com.ax.associator.store.domain.memberDto;


import org.apache.shiro.util.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ax.associator.store.service.storeAssociatorService;
import org.springframework.transaction.annotation.Transactional;


import java.lang.reflect.Member;
import java.util.*;

@Service
public class storeAssociatorServiceImpl implements storeAssociatorService {

    @Autowired
    private BaseAssociatorMapper baseAssociatorMapper;

    @Autowired
    private BusiAttachRelationMapper busiAttachRelationMapper;

    @Autowired
    private AttachmentMapper attachmentMapper;

    /**
     * 会员添加
     * @param baseAssociator
     * @return
     */
    @Override
    @Transactional
    public Integer insertSelective(BaseAssociator baseAssociator) {
        Integer integer=0;
        String phone= baseAssociator.getPhone();
        String personId=baseAssociator.getPersonId();
        Integer integer1= baseAssociatorMapper.personId(personId);
        if(integer1 == 0){
            Integer integer2=baseAssociatorMapper.phone(phone);
            if(integer2 == 0){
                integer = baseAssociatorMapper.insertSelective(baseAssociator);
                String attachmentUrl=baseAssociator.getAttachmentUrl();
                Attachment attachment=new Attachment();
                attachment.setAttachmentUrl(attachmentUrl);
                attachmentMapper.insertSelective(attachment);
                BusiAttachRelation busiAttachRelation=new BusiAttachRelation();
                busiAttachRelation.setAttachmentId(attachment.getAttachmentId());
                busiAttachRelation.setBusinessSeq(String.valueOf(baseAssociator.getAssociatorId()));
                busiAttachRelationMapper.insertBusiAttachRelation(busiAttachRelation);
            }else {
                throw new IllegalArgumentException("手机号已被注册");
            }
        }else{
            throw new IllegalArgumentException("身份证号已被注册");
        }
        return  integer;
    }

    @Override
    public List<BaseAssociator> queryAssociatorMapper(Map<String, Object> map) {
        List<BaseAssociator> list= baseAssociatorMapper.queryAssociatorMapper(map);
        return list;
    }

    @Override
    public PageUtils queryMember(Map<String, Object> params) {
        Query query=new Query(params);
        List<memberDto> list=baseAssociatorMapper.queryMember(query);
        for(memberDto li:list){
            Integer age = BirthUtil.getAgeByBirth(li.getBirth()) ;
            li.setAge(age);
        }
        Integer count=baseAssociatorMapper.countMember();
        PageUtils pageUtils=new PageUtils(list,count);
        return pageUtils;
    }

    @Override
    @Transactional
    public Integer updateBaseAssociator(BaseAssociator baseAssociator) {
        Integer integer=0;

        String phone=baseAssociator.getPhone();
        String personId=baseAssociator.getPersonId();
        Long associatorId=baseAssociator.getAssociatorId();
        Integer count=baseAssociatorMapper.queryUpdateCount(associatorId,personId,phone);
        if(count==0){
            Integer integer1= baseAssociatorMapper.personId(personId);
            if(integer1==0){
                Integer integer2=baseAssociatorMapper.phone(phone);
                if(integer2==0){
                    integer = baseAssociatorMapper.updateBaseAssociator(baseAssociator);
                }else{
                    throw new IllegalArgumentException("手机号已被注册");
                }
            }else{
                throw new IllegalArgumentException("身份证号已被注册");
            }
        }else{
            baseAssociatorMapper.updateBaseAssociator(baseAssociator);
            throw new IllegalArgumentException("修改成功");
        }


        return integer;
    }

    @Override
    @Transactional
    public Integer deleteAssociator(Long associatorId) {
        Assert.notNull(associatorId,String.format(ErrorUtil.PARAMETER_NULL, "associatorId"));
        Integer integer=baseAssociatorMapper.deleteAssociator(associatorId);
        return integer;
    }

}
